package com.rangers.auth.sample.jpa.convert;

import java.util.Set;

import javax.persistence.AttributeConverter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.rangers.auth.base.utils.JsonUtils;
import com.rangers.auth.sample.jpa.entity.Menu;


/**
 * 序列化转换菜单
 * @version 1.0
 * @author fuhw
 * @date 2017年8月18日 下午6:44:24
 */
public class SetMenuConverter implements AttributeConverter<Set<Menu>, String> {

	/* (non-Javadoc)
	 * @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object)
	 */
	@Override
	public String convertToDatabaseColumn(Set<Menu> attribute) {
		return JsonUtils.writeValueAsString(attribute);
	}

	/* (non-Javadoc)
	 * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
	 */
	@Override
	public Set<Menu> convertToEntityAttribute(String dbData) {
		return JsonUtils.readValue(dbData, new TypeReference<Set<Menu>>() {});
	}


}
